package sn.gandal.agriculture.agrimobile.j2me.businessObjects;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author dysow
 */
public class Paysans
{

    private int id;
    private int idAdp = -1;
    private int idPays = 1;
    private Vector listAbonneManaged = new Vector();

    public Paysans()
    {
    }

    public Paysans(int idAdp)
    {
	this.idAdp = idAdp;
    }

    public Paysans(int idAdp, int idPays)
    {
	this(idAdp);

	this.idPays = idPays;
    }

    /**
     * @return the id
     */
    public int getId()
    {
	return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id)
    {
	this.id = id;
    }

    /**
     * @return the idAdp
     */
    public int getIdAdp()
    {
	return idAdp;
    }

    /**
     * @param idAdp the idAdp to set
     */
    public void setIdAdp(int idAdp)
    {
	this.idAdp = idAdp;
    }

    /**
     * @return the idPays
     */
    public int getIdPays()
    {
	String id = this.idAdp + "";
	return Integer.parseInt(id.substring(0, 2));
//	return idPays;
    }

    /**
     * @param idPays the idPays to set
     */
    public void setIdPays(int idPays)
    {
	this.idPays = idPays;
    }

    /**
     * Méthode pour sérializer l'Agent de Pesée.
     * @return
     * @throws IOException
     */
    public byte[] serialize() throws IOException
    {
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	DataOutputStream dout = new DataOutputStream(bout);

	dout.writeInt(this.idAdp);
	dout.writeInt(this.idPays);

	dout.flush();
	dout.close();
	bout.close();

	return bout.toByteArray();
    }

    /**
     * Méthode pour dérésializer l'Agent de Pesée.
     * @param data
     * @throws IOException
     */
    public void deSerialize(byte[] data) throws IOException
    {
	ByteArrayInputStream bin = new ByteArrayInputStream(data);
	DataInputStream din = new DataInputStream(bin);

	this.idAdp = din.readInt();
	this.idPays = din.readInt();

	din.close();
	bin.close();
    }

    public boolean validerAgentDePesee()
    {
	return ((this.idAdp < 10000) && (this.idAdp > 999));
    }

    public void addAbonneInList(Membre abonne)
    {
	listAbonneManaged.addElement(abonne);
    }

    public Vector getListAbonneManaged()
    {
	return listAbonneManaged;
    }
}
